package com.house.tgroup.service.impl;

import com.house.common.utils.R;
import com.house.tgroup.dao.GroupDao;
import com.house.tgroup.dao.GroupDescDao;
import com.house.tgroup.entity.GroupEntity;
import com.house.tgroup.feign.FeignService;
import com.house.tgroup.vo.UnitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.house.common.utils.PageUtils;
import com.house.common.utils.Query;
import com.house.tgroup.entity.GroupDescEntity;
import com.house.tgroup.service.GroupDescService;
import org.springframework.transaction.annotation.Transactional;

@Service("groupDescService")
@SuppressWarnings("all")
public class GroupDescServiceImpl extends ServiceImpl<GroupDescDao, GroupDescEntity> implements GroupDescService {

    @Autowired
    private GroupDescDao groupDescDao;
    @Autowired
    private FeignService feignService;
    @Autowired
    private GroupDao groupDao;
    //添加团购  判断 人数是否已满
    //修改团购数量
    //添加详情表记录
    //传id 和当前登录人名字
    //后台做添加
    @Override
    @Transactional
    public R addUser(Integer id, String uname, BigDecimal submoney) {
        GroupEntity groupEntity = groupDao.selectById(id);
        groupEntity.setPeoCont(groupEntity.getPeoCont()+1);
        GroupDescEntity g = new GroupDescEntity();
        //feign调用查出手机号
        String phone = feignService.findByUserName(uname);
        //查询需要的户型
       UnitVo unitVo= groupDescDao.selb(id);
        g.setGroupId(id);
        //当前时间
        g.setJoinTime(new Date());
        g.setPhone(phone);
        //添加团购默认为0 0为团购中
        g.setStatus(0);
        //前台输入交付定金金额
        g.setSubmoney(submoney);
        g.setUnitId(unitVo.getUnitId());
        g.setUnitName(unitVo.getUnitName());
        g.setUserName(uname);
        baseMapper.insert(g);
        groupDao.updateById(groupEntity);
        return R.ok();
    }

    @Override
    public R findAllByIdAndName(Integer id, String name) {
        QueryWrapper<GroupDescEntity> w = new QueryWrapper<>();
        w.eq("group_id",id);
        w.eq("user_name",name);
        GroupDescEntity gde = baseMapper.selectOne(w);
        if (gde!=null){
            return R.ok();
        }
        return R.error();
    }

}